package com.pai.service.AppUser;

import com.pai.dao.AppUser.AppUserDao;
import com.pai.dao.AppUser.AppUserDaoImpl;
import com.pai.entity.AppUser;
import com.pai.dao.BaseDao;
import com.pai.entity.vmodel.UserInfo;


import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

public class AppUserServiceImpl implements AppUserService {
    private AppUserDao theDao;  //部门dao

    public AppUserServiceImpl() {
        this.theDao = new AppUserDaoImpl();
    }

    /**
     * 获取列表
     * @return
     */
    @Override
    public List<AppUser> getList() {
        Connection connection = null;
        connection = BaseDao.getConnection();
        List<AppUser> resultList = new ArrayList<>();
        try {
            resultList = theDao.getList(connection);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            BaseDao.closeResource(connection, null, null);
        }
        return resultList;
    }
    /**
     * 获取项详情
     * @param id
     * @return
     */
    @Override
    public AppUser getItemDetail(String id) {
        Connection connection = null;
        connection = BaseDao.getConnection();
        AppUser item = new AppUser();
        try {
            item = theDao.getItemDetail(connection, id);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            BaseDao.closeResource(connection, null, null);
        }
        return item;
    }
    /**
     * 新增
     * @param item
     * @return
     */
    @Override
    public int add(AppUser item) {
        Connection connection = null;
        connection = BaseDao.getConnection();
        int appUserId = 0;

        try {
            appUserId = theDao.add(connection, item);
        } catch (Exception e) {
            e.printStackTrace();
        }
        BaseDao.closeResource(connection, null, null);
        return appUserId;
    }



    /**
     * 更新
     * @param item
     * @return
     */
    @Override
    public boolean update(AppUser item) {
        Connection connection = null;
        connection = BaseDao.getConnection();
        boolean flag = false;

        try {
            flag = theDao.update(connection, item) > 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        BaseDao.closeResource(connection, null, null);
        return flag;
    }

    /**
     * 更新用户角色
     * @param personnelId  用户id
     * @param roleName 要转换成的角色名称
     * @return
     */
    public boolean updateRole(String personnelId, String roleName) {
        Connection connection = null;
        connection = BaseDao.getConnection();
        boolean flag = false;

        try {
            flag = theDao.updateRole(connection, personnelId, roleName) > 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        BaseDao.closeResource(connection, null, null);
        return flag;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    public boolean delete(String id) {
        Connection connection = null;
        connection = BaseDao.getConnection();
        boolean flag = false;

        try {
            flag = theDao.delete(connection, id) > 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        BaseDao.closeResource(connection, null, null);
        return flag;
    }

    /**
     * 登录
     * @param appUser
     * @return
     */
    @Override
    public AppUser login(AppUser appUser) {
        Connection connection = null;
        connection = BaseDao.getConnection();
        AppUser item = null;
        try {
            item = theDao.login(connection, appUser);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            BaseDao.closeResource(connection, null, null);
        }
        return item;
    }

    /**
     * 获取用户信息
     * @param account 账号
     * @return
     */
    @Override
    public UserInfo getUserInfo(String account) {
        Connection connection = null;
        connection = BaseDao.getConnection();
        UserInfo item = null;
        try {
            item = theDao.getUserInfo(connection, account);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            BaseDao.closeResource(connection, null, null);
        }
        return item;
    }
}
